﻿Imports System.Text

Public Class SqlBuilder

    Private _conditionCount As Integer = 0
    Private _sql As New StringBuilder

    Public Sub New(ByVal selectStatement As String)
        _sql.Append(selectStatement)
    End Sub

    Public Sub AppendCriteria(ByVal criteria As String, ByVal ParamArray args() As String)
        If Not String.IsNullOrEmpty(criteria) Then

            _conditionCount += 1
            If _conditionCount = 1 Then
                _sql.Append(" WHERE ")
            Else
                _sql.Append(" AND ")
            End If
            _sql.Append(String.Format(criteria, args))
        End If
    End Sub

    Public Overrides Function ToString() As String
        Return _sql.ToString()
    End Function

    Public Shared Function GetSafeWildCardValue(ByVal value As String) As String
        Return value.Replace("'", "''").Replace("*", "%")
    End Function

End Class
